<template>
  <div>
    <div class="titlebox">
        <el-button type="primary" size="mini" @click="handleAdd">添加</el-button>
        <el-button type="danger" size="mini" @click="handleMulDel('more')">删除</el-button>
      <div>
         <el-form>
            <el-form-item label="输入ID查询：">
              <el-input
                v-model="id"
                placeholder="请输入ID">
              </el-input>
            </el-form-item>
          </el-form>
      </div> 
        <el-button type="primary" size="mini" @click="search">查询</el-button>
    </div>
 
  <el-table
    ref="multipleTable"
    :data="tableData"
    v-loading="loading"
    tooltip-effect="dark"
    style="width: 100%"
    @selection-change="handleSelectionChange">
    <el-table-column
      type="selection"
  >
    </el-table-column>
  <el-table-column
      prop="news_id"
      label="news_id"
  >
  </el-table-column>
     <el-table-column
      prop="title"
      label="标题"
  >
    </el-table-column>
    <el-table-column
      prop="content"
      label="目录"
  >
    </el-table-column>
    <el-table-column
      prop="keywords"
      label="关键词"
      show-overflow-tooltip>
    </el-table-column>
    <el-table-column
      prop="author"
      label="作者"
      show-overflow-tooltip>
    </el-table-column>

    <el-table-column
      label="操作"
    >
    <template slot-scope="scope">
      <el-button type="danger" size="mini" @click="handleMulDel('row',scope.row.news_id)">删除</el-button>
      <el-button type="primary" size="mini"  @click="amendbtn(scope.row,scope.row.news_id)">修改</el-button>
    </template>
    </el-table-column>
  </el-table>
    <!-- 分页start -->
    <div class="fy" v-if="tableData.length">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="this.page"
      :page-sizes="[5, 10, 15, 50]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
    <!-- 分页 end -->
  <!-- 添加弹框start -->
  <el-dialog   
  title="添加"
  :visible.sync="dialogVisible"
  width="80%"
  :before-close="handleClose">
  <el-form :model="addQuery" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
              <el-input v-model="addQuery.title" placeholder="请输入标题"></el-input>
            </el-form-item>
            <el-form-item label="目录" prop="content">
              <el-input type="content" placeholder="请输入目录" v-model="addQuery.content"></el-input>
            </el-form-item>
            <el-form-item label="关键词" prop="keywords">
              <el-input type="keywords" placeholder="请输入关键词" v-model="addQuery.keywords"></el-input>
            </el-form-item>
          <el-form-item label="缩略图" prop="thumb">
              <el-input placeholder="请输入缩略图" v-model="addQuery.thumb"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
              <el-input placeholder="请输入作者" v-model="addQuery.author"></el-input>
        </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addFormData">确 定</el-button>
  </span>
</el-dialog>
  <!-- 添加弹框end -->

  <!-- 修改弹框start -->
  <el-dialog
  title="修改"
  :visible.sync="dialogrevise"
  width="80%"
  :before-close="handleClose">
   <el-form :model="addQuery" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
              <el-input type="title" placeholder="请输入标题" v-model="addQuery.title"></el-input>
            </el-form-item>
            <el-form-item label="目录" prop="content">
              <el-input type="content" placeholder="请确认目录" v-model="addQuery.content"></el-input>
            </el-form-item>
          <el-form-item label="关键词:" prop="keywords">
              <el-input placeholder="请输入发布时间" v-model="addQuery.keywords"></el-input>
        </el-form-item>
          <el-form-item label="略缩图" prop="thumb">
              <el-input placeholder="请输入点击数" v-model="addQuery.thumb"></el-input>
          </el-form-item>
          <el-form-item label="作者" prop="author">
              <el-input placeholder="请输入点击数" v-model="addQuery.author"></el-input>
          </el-form-item>
  </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogrevise = false">取 消</el-button>
    <el-button type="primary" @click="amendData">确 定</el-button>
  </span>
</el-dialog>
  <!-- 修改弹框end -->
  </div>
</template>

<script>
import {post,get} from '../uilt/http.js'
import {list2,add2,changestatus,del2,edit2} from '../uilt/app.js'
export default {
      data() {
      return {
        loading:true,
        tableData: [],
        adid:'',
        page:1,
        pageSize:6,
        id:'',
        multipleSelection: [],
        dialogVisible:false,
        dialogrevise:false,
        addQuery:{
            title: "", 
            content: "", 
            keywords: "", 
            thumb: "", 
            author: ""
        },
        total:0,
       rules: {
        news_id: [
          { 
          required: true, 
          message: "请输入news_id", 
          trigger: "blur" 
          },
          { 
            min: 3,
            max: 5, 
            message: "长度在 3 到 5 个字符", 
            trigger: "blur" },
        ],
        title: [
          { 
          required: true,
           message: "请输入标题", 
           trigger: "blur" 
           },
        ],
        content: [
          { 
          required: true,
           message: "请输入目录", 
           trigger: "blur" 
           },
        ],
         add_time: [
          { 
          required: true,
           message: "请输入发布时间", 
           trigger: "blur" 
           },
        ],
         hits: [
          { 
          required: true,
           message: "请输入点击数", 
           trigger: "blur" 
           },
        ],
      },
      }
    },
mounted(){
    this.onSearch()
},
    methods: {
       async search(){
            let res = await this.$http.get('/visit/news/show',{id:this.id})
            if(res.code == '00000'){
                  this.tableData=[]
                  this.tableData.push(res.data)          
            }
        },
    //  渲染列表
     async onSearch(){
          let res =await get(list2,{page:this.page,pageSize:this.pageSize})
          if(res.code=='00000'){
              this.tableData=res.data;
                this.loading = false;
              this.total=res.total;
          }
      },
        // 添加表单数据
        async addFormData(){
           let res = await post(add2,{   
          "title": this.addQuery.title, 
          "content":  this.addQuery.content, 
           "keywords":  this.addQuery.keywords, 
          "thumb":  this.addQuery.thumb, 
          "author":  this.addQuery.author, 
            })
        if(res.code=='00000'){
            this.$message.success('添加成功')
            this.onSearch()
            this.dialogVisible = false
            this.addQuery={}
     }else{
            this.$message.error('添加错误')
        }
    },
      // 批量删除
     handleMulDel(type,news_id){
      //  console.log(news_id);
      if(type==='more'){
         if(this.multipleSelection.length === 0) {
            return this.$message.error('请勾选要删除的数据')
        }
        let arr = [] 
       this.multipleSelection.forEach(item =>{
        arr.push(item.news_id)
       });
       news_id = arr
      }
   //把批量删除的item_id 传给后台
       this.$confirm('此操作将永久删除这条数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          }).then(async() => {
        let res = await get(del2,{news_id:news_id})
        // console.log(res);
        if(res.code=='00000'){
            this.$message({
                type: 'success',
                message: '删除成功!'
             });
             this.onSearch()
        }
            }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 修改适用状态
      async changeStatus(row){
        let res = await post(changestatus,
        {id:row.id,is_use:row.is_use})
        row.is_use != row.is_use;
        if(res.code=='00000'){
            this.$message.success("修改状态成功");
        }else{
            this.$message.error("修改状态失败");
        }
    },
    // 修改
    amendbtn(row,news_id){
      this.adid = news_id
      // console.log(this.adid);
      this.addQuery = row
      // console.log(this.addQuery);
      this.dialogrevise=true;
      },
    async amendData(){
      let res = await post(edit2,{id:this.adid,
      "formdata":{
          "title": this.addQuery.title, 
          "content":  this.addQuery.content, 
          "keywords":  this.addQuery.keywords, 
          "thumb":  this.addQuery.thumb, 
          "author":  this.addQuery.author, 
      }})
      // console.log(res);
      if(res.code == '00000'){
        this.$message.success('修改成功')
        this.onSearch()
        this.dialogrevise = false
        this.addQuery={}
      }else{
            this.$message.error('修改失败')
        }
    },
      handleAdd(){
        this.dialogVisible=true;
      },
      
     handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
         handleSizeChange(val) {
             this.pageSize =val;
               this.onSearch()
      },
      handleCurrentChange(val) {
         //  console.log(val)
          this.page = val;
           this.onSearch();
      }
    }
}
</script>

<style scoped>
.el-table{
    margin-top: 16px;
}
.btn{
    display: flex;
}
.titlebox{
  height: 30px;
  display: flex;
}
.titlebox .el-form-item{
  display: flex;
  margin-left: 30px;
}
.titlebox .el-form-item .el-input{
  width: 190px;
  height: 30px;
}
.titlebox .el-button{
  margin-left: 30px;
}
.fy{
  padding-top: 25px;
  text-align: center;
}
</style>